const { watch, src, dest, parallel, series } = require('gulp');
// const babel = require('gulp-babel');
// const uglify = require('gulp-uglify');
const sass = require('gulp-sass')
// const gulpClean = require('gulp-clean')
const rename = require('gulp-rename')
const cleanCss = require('gulp-clean-css')

function clean() {
    return src('dist', {allowEmpty: true})
    .pipe(gulpClean())
}

function allowFile() {
    return src('src/**/*.{png,jpeg,jpg,json,wxml,js,wxs,wxss}')
        .pipe(dest('dist/'))
}

// function js() {
//     return src('src/**/*.js')
//         .pipe(babel({
//             presets: ['@babel/preset-env']
//         }))
//         .pipe(uglify())
//         .pipe(dest('src/'))
// }

function scss() {
    return src('src/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        // .pipe(cleanCss({
        //     // format: process.env.NODE_ENV === 'production' ? 'inline' : 'beautify'
        // }))
        .pipe(rename({
            extname: '.wxss'
        }))
        .pipe(dest('src/'))
}

function watchAll() {
    watch('src/**/*.scss', parallel(scss))
}

// 默认命令
exports.default = parallel(scss)
// exports.default = series(
//     clean,
//     parallel(allowFile, scss, js)
// )
// 监听文件修改
exports.watch = watchAll

// 部署线上，生成一个dist文件，移除scss